package controller

import (
	"background/db"
	"background/logs"
	"background/model"
	"fmt"
	"log"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

type PlanController struct {
}

func (t *PlanController) GetPlan(c *gin.Context) {

}

func (t *PlanController) GetPlanDates(c *gin.Context) {
	type Req struct {
		Date string `json:"date"`
	}
	var req Req

	resp := RespBase{
		Msg:    "ERROR",
		Status: 23,
	}
	defer c.JSON(200, &resp)
	e := c.BindJSON(&req)
	if nil != e {
		log.Print(e.Error())
		return
	}
	plan := []model.Plan{}
	e = db.GetOrm().Model(&plan).Where("date = ?", req.Date).Find(&plan).Error
	if nil != e {
		log.Print(e.Error())
	}
	resp.Status = 0
	resp.Msg = "OK"
	resp.Data = plan
	return
}

func (t *PlanController) AddPlan(c *gin.Context) {
	var req model.Plan
	resp := RespBase{Msg: "r", Status: 0, Data: nil}
	defer c.JSON(200, &resp)
	e := c.BindJSON(&req)
	if nil != e {
		log.Print(e.Error())
		return
	}
	e = db.GetOrm().Create(&req).Error
	if nil != e {
		log.Print(e.Error())
		return
	}

	resp.Msg = "OK"
	resp.Status = 0
}

func (t *PlanController) DelPlan(c *gin.Context) {
	resp := RespBase{Msg: "r", Status: 0, Data: nil}
	defer c.JSON(200, &resp)
	var e error
	iid := 0
	id := c.Query("id")
	if "" != id {
		iid, e = strconv.Atoi(id)
		if nil != e {
			log.Print(e.Error())
			return
		}
	} else {
		return
	}
	plan := model.Plan{}
	plan.ID = uint32(iid)
	e = db.GetOrm().Delete(&plan).Error
	if nil != e {
		log.Print(e.Error())
		return
	}
	resp.Msg = "OK"
	resp.Status = 0
}

type ReqDate struct {
	Date string `gorm:"date" json:"date"`
}

func (m *ReqDate) TableName() string {
	return "plan"
}

func (t *PlanController) PlanDay(c *gin.Context) {
	var req ReqDate
	resp := RespBase{
		Msg:    "ERROR",
		Status: 23,
	}
	defer c.JSON(200, &resp)

	e := c.BindJSON(&req)
	if nil != e {
		log.Print(e.Error())
		return
	}
	ret := make([]ReqDate, 1)
	sql := fmt.Sprintf(`SELECT distinct plan.date FROM plan where DATE_FORMAT(plan.date,'%%Y-%%m')=DATE_FORMAT('%s','%%Y-%%m')`, req.Date)
	log.Print(sql)
	e = db.GetOrm().Raw(sql).Find(&ret).Error
	if nil != e {
		log.Print(e.Error())
		return
	}
	resp.Data = ret
	resp.Msg = "OK"
	resp.Status = 0
}

func (t *PlanController) Types(c *gin.Context) {
	resp := RespBase{
		Msg:    "ERROR",
		Status: 23,
	}
	defer c.JSON(200, &resp)
	rets := []model.PlanType{}
	e := db.GetOrm().Model(&model.PlanType{}).Find(&rets).Error
	if nil != e {
		log.Print(e.Error())
		return
	}
	resp.Data = rets
	resp.Msg = "OK"
	resp.Status = 0
}

func (t *PlanController) CreateUndo(c *gin.Context) {
	resp := RespBase{}
	defer func() {
		c.JSON(200, resp)
	}()
	var req model.Undo
	e := c.BindJSON(&req)
	if nil != e {
		logs.Error(e.Error())
		resp.Msg = "wrong input"
		return
	}
	req.CreateTime = time.Now().Format("2006-01-02 15:04:05")
	e = db.GetOrm().Model(&model.Undo{}).Create(&req).Error
	if nil != e {
		log.Print(e.Error())
		return
	}
	log.Print(req.ID)
	resp.Data = req.ID
	resp.Msg = "OK"
	resp.Status = 0
}

func (t *PlanController) UnFinishUndo(c *gin.Context) {
	resp := RespBase{}
	defer func() {
		c.JSON(200, resp)
	}()
	id := c.Param("id")

	e := db.GetOrm().Exec(fmt.Sprintf("update background.`undo` set done = '0' where id = '%s'",id )).Error
	if nil != e {
		logs.Error(e.Error())
		return
	}
	resp.Data = nil
	resp.Msg = "OK"
	resp.Status = 0
}

func (t *PlanController) FinishUndo(c *gin.Context) {
	resp := RespBase{}
	defer func() {
		c.JSON(200, resp)
	}()
	id := c.Param("id")
	log.Print("finish undo id",id)

	e := db.GetOrm().Exec(fmt.Sprintf("update background.`undo` set done = 1,finish_time='%s' where id = %s",
			time.Now().Format("2006-01-02 15:04:05"),id)).Error
	log.Print(fmt.Sprintf("update background.`undo` set done = 1,finish_time='%s' where id = %s",time.Now().Format("2006-01-02 15:04:05"),id))
	if nil != e {
		logs.Error(e.Error())
		return
	}
	resp.Data = nil
	resp.Msg = "OK"
	resp.Status = 0
}

func (t *PlanController) UpdateUndo(c *gin.Context) {
	resp := RespBase{}
	defer func() {
		c.JSON(200, resp)
	}()
	var req model.Undo
	e := c.BindJSON(&req)
	if nil != e {
		resp.Msg = "wrong input"
		return
	}
	e = db.GetOrm().Model(&model.Undo{}).Update(&req).Error
	if nil != e {
		logs.Error(e.Error())
		return
	}
	resp.Data = nil
	resp.Msg = "OK"
	resp.Status = 0
}

func (t *PlanController) GetDone(c *gin.Context) {
	resp := RespBase{}
	defer func() {
		c.JSON(200, resp)
	}()
	undos := []model.Undo{}
	e := db.GetOrm().Model(&model.Undo{}).Where("done = 1").Find(&undos).Error
	if nil != e {
		log.Print(e.Error())
		return
	}
	resp.Data = undos
	resp.Msg = "OK"
	resp.Status = 0
}

func (t *PlanController) GetUndo(c *gin.Context) {
	resp := RespBase{}
	defer func() {
		c.JSON(200, resp)
	}()
	undos := []model.Undo{}
	e := db.GetOrm().Model(&model.Undo{}).Find(&undos).Error
	if nil != e {
		log.Print(e.Error())
		return
	}
	resp.Data = undos
	resp.Msg = "OK"
	resp.Status = 0
}

func (t *PlanController) GetPageUndo(c *gin.Context) {
	type ReqGetPageUndo struct {
	}
	var req ReqGetPageUndo
	resp := RespBase{}
	// limit := c.Query("limit")
	// offset := c.Query("offset")

	// iLmit, e := strconv.Atoi(limit)
	// if nil != e {
	// 	return
	// }
	// iOffset, e := strconv.Atoi(offset)
	// if nil != e {
	// 	return
	// }
	defer func() {
		c.JSON(200, resp)
	}()
	e := c.BindJSON(&req)
	if nil != e {
		logs.Error(e.Error())
		return
	}

}

func (t *PlanController) DeleteUndo(c *gin.Context) {
	resp := RespBase{}
	defer func() {
		c.JSON(200, resp)
	}()
	sid := c.Param("id")
	id := db.Atoi(sid)
	e := db.GetOrm().Model(&model.Undo{}).Delete(&model.Undo{ID: int32(id)}).Error
	if nil != e {
		log.Print(e.Error())
		return
	}
	resp.Status = 0
	resp.Msg = "OK"
}
